<script lang="ts" setup>
import { useLazyData } from '@/hooks'
import useStore from '@/store'
import { useIntersectionObserver } from '@vueuse/core'
import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import HomePanel from './home-panel.vue'
import HomeSkeleton from './home-skeleton.vue'

// 数据懒加载：当页面的某一块区域到达可视区的时候
// 采取请求这个可视区中所有的接口

const { home } = useStore()
const { getGoodsNewList } = home
const { newGoodsList } = storeToRefs(home)
// #1 注释掉
// getGoodsNewList()

const target = useLazyData(() => {
  setTimeout(() => {
    getGoodsNewList()
  }, 6000)
})
// #2 观察 target 是否进入可视区
/* const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
  // 如果观测的目标 target 到达了可视区 isIntersecting 就是 true
  if (isIntersecting) {
    // #3 采取调用我的接口，拿数据
    getGoodsNewList()
    stop()
  }
}) */
</script>
<template>
  <div class="home-new">
    <HomePanel title="新鲜好物" sub-title="新鲜出炉 品质靠谱" ref="target">
      <template #right><XtxMore path="/" /></template>
      <transition name="fade">
        <!-- 面板内容 -->
        <ul class="goods-list" v-if="newGoodsList.length > 0">
          <li v-for="item in newGoodsList" :key="item.id">
            <RouterLink to="/">
              <img v-lazy="item.picture" alt="" />
              <p class="name ellipsis">{{ item.desc }}</p>
              <p class="price">&yen;{{ item.price }}</p>
            </RouterLink>
          </li>
        </ul>
        <HomeSkeleton v-else />
      </transition>
    </HomePanel>
  </div>
</template>

<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>
